package org.come.init;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class UpdateController {
    private Boolean needUpdate = false;
    private Boolean failure = false;
    private Boolean done = false;
    private JSONObject jsonObject;
    private String version;
    private UpdateView updateView;

    public UpdateController(UpdateView updateView) {
        this.updateView = updateView;
    }

    public void start() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        version = getVersion();
        if (version != null) {
        	
        	// 需要服务器端加了对应更新代码以后放开 燕赵阿祖 2022年10月8日17:37:55
            String results = Tool.getWebCon("http://119.91.31.129:7777/request/requests.php?type=CheckUpdate&version=" + version);
//        	String results = "successful";
        	
            if (results != null) {
                jsonObject = Tool.StrintToJson(results);
                if (jsonObject != null) {
                    try {
                        if (jsonObject.getString("results").equals("successful")) {
                            needUpdate = true;
                            if (startUpdate()) {
                                 updateView.getTitle().setText("更新完成，祝你游戏愉快！");
                                done = true;
                            } else {
                                updateView.getTitle().setText("更新失败！请到QQ群下载完整版本。");
                                failure = true;
                            }
                        } else {
                            updateView.getTitle().setText(jsonObject.getString("results"));
                            done = true;
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    //System.out.println("检查更新失败！");
                }
            } else {
                failure = true;
            }
        }
        
    }

    public Boolean isFailure() {
        return failure;
    }

    public Boolean isDone() {
        return done;
    }

    public Boolean isNeedUpdate() {
        return needUpdate;
    }

    public Boolean startUpdate() {
        if (isNeedUpdate() && updateView != null) {
            try {
                JSONArray JSONArray = jsonObject.getJSONArray("versions");
                for (int i = 0; i < JSONArray.length(); i++) {
                    JSONArray list = (org.json.JSONArray) JSONArray.get(i);
                      updateView.getTitle().setText("开始下载更新包:" + list.get(0));
                    new UpdatePack((String) list.get(0), updateView, Long.parseLong(list.get(1) + ""));
                }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
                //System.out.println("检查更新失败！");
                return false;
            }
        }
        return false;
    }

    public String getVersion() {
        String cen = Tool.readToString(System.getProperty("user.dir") + "/update/version.json");
        if (cen == null) return null;
        JSONObject jsonObject = Tool.StrintToJson(cen);
        if (jsonObject != null && jsonObject.has("version")) {
            try {

                return jsonObject.getString("version");
            } catch (JSONException e) {
                return null;
            }
        } else {
            return null;
        }
    }
}

